﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Sapper
{
    public partial class Form1 : Form
    {
        static int matrX;
        static int matrY;
        static byte bombAmount;
        static byte bombCounter = 0;
        SpecialButton[,] spButt = new SpecialButton[matrX, matrY];
        static int timeCounter=0;
        static bool timerFlag = true;
        static bool winFlag;
        static Form myForm;
        static Form myForm1;
        static TextBox myTextBox;
        static string line1;
        static string line2;
        static string line3;
        static string userName;

        public static int MatrX
        {
            get
            {
                return matrX;
            }
            set
            {
                matrX=value;
            }
        }

        public static int MatrY
        {
            get
            {
                return matrY;
            }
            set
            {
                matrY = value;
            }
        }

        public static byte BombAmount
        {
            get
            {
                return bombAmount;
            }
            set
            {
                bombAmount = value;
            }
        }

        public static byte BombCounter
        {
            get
            {
                return bombCounter;
            }
            set
            {
                bombCounter = value;
            }
        }

        private void ShowRegistration()
        {
            myForm = new Form();
            myForm.Text = "Registration";
            myForm.SetBounds(10, 10, 200, 200);
            myForm.Height = 150;

            Label myLabel = new Label();
            myLabel.Left = 5;
            myLabel.Top = 10;
            myLabel.Text = "Enter your name:";
            myForm.Controls.Add(myLabel);

            myTextBox = new TextBox();
            myTextBox.Text = "User";
            myTextBox.Left = 5;
            myTextBox.Top = 35;
            myForm.Controls.Add(myTextBox);

            Button myOkButton = new Button();
            myOkButton.Text = "Ok";
            myOkButton.Left = 5;
            myOkButton.Top = 75;
            myOkButton.MouseDown += new MouseEventHandler(MyOkMouseDown);
            myForm.Controls.Add(myOkButton);

            myForm.Show();
        }

        private void ShowRecords()
        {
            myForm1 = new Form();
            myForm1.Text = "Records";
            myForm1.SetBounds(10, 10, 200, 200);
            myForm1.Height = 170;
            myForm1.Width = 250;

            Label begLabel = new Label();
            begLabel.Left = 5;
            begLabel.Top = 10;
            begLabel.Text = "Beginner:   ";
            myForm1.Controls.Add(begLabel);

            Label amaLabel = new Label();
            amaLabel.Left = 5;
            amaLabel.Top = 35;
            amaLabel.Text = "Amateur:   ";
            myForm1.Controls.Add(amaLabel);

            Label profLabel = new Label();
            profLabel.Left = 5;
            profLabel.Top = 60;
            profLabel.Text = "Professional:   ";
            myForm1.Controls.Add(profLabel);

            Label begLabel1 = new Label();
            begLabel1.Left = 110;
            begLabel1.Top = 10;
            begLabel1.Text = line1;
            myForm1.Controls.Add(begLabel1);

            Label amaLabel1 = new Label();
            amaLabel1.Left = 110;
            amaLabel1.Top = 35;
            amaLabel1.Text = line2;
            myForm1.Controls.Add(amaLabel1);

            Label profLabel1 = new Label();
            profLabel1.Left = 110;
            profLabel1.Top = 60;
            profLabel1.Text = line3;
            myForm1.Controls.Add(profLabel1);
            
            
            Button myOk1Button = new Button();
            myOk1Button.Text = "Ok";
            myOk1Button.Left = 80;
            myOk1Button.Top = 100;
            myOk1Button.MouseDown += new MouseEventHandler(MyOk1MouseDown);
            myForm1.Controls.Add(myOk1Button);

            Button myClearButton = new Button();
            myClearButton.Text = "Clear results";
            myClearButton.Left = 5;
            myClearButton.Top = 100;
            myClearButton.MouseDown += new MouseEventHandler(MyClearMouseDown);
            myForm1.Controls.Add(myClearButton);


            myForm1.Show();
        }

        private void SaveToFile()
        {
            StreamWriter sw = new StreamWriter("SaperRecords.txt");
            
                sw.WriteLine("User  999 sec");
                sw.WriteLine("User  999 sec");
                sw.WriteLine("User  999 sec");
            
            sw.Close();
        }

        private void SaveToFile(int sec)
        {
           // try
           // {

                //Pass the filepath and filename to the StreamWriter Constructor
                LoadFromFile();
                StreamWriter sw = new StreamWriter("SaperRecords.txt");
                //StreamReader inStream = null;
                //string line;

                if (beginnerToolStripMenuItem.Checked == true)
                {
                    sw.WriteLine(userName + "  " + Convert.ToString(sec) + " sec");
                    sw.WriteLine(line2);
                    sw.WriteLine(line3);
                }

                if (amateurToolStripMenuItem.Checked == true)
                {
                    sw.WriteLine(line1);
                    sw.WriteLine(userName + "  " + Convert.ToString(sec) + " sec");
                    sw.WriteLine(line3);
                }

                if (professionalToolStripMenuItem.Checked == true)
                {
                    sw.WriteLine(line1);
                    sw.WriteLine(line2);
                    sw.WriteLine(userName+"  "+Convert.ToString(sec) + " sec");
                }
                

                sw.Close();
           /* }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e.Message);
            }
            finally
            {
                Console.WriteLine("Executing finally block.");
            }*/
        }

        private void LoadFromFile()
        {
           // string textLine;
            StreamReader inStream = null;

          //  try
           // {
                FileInfo textFile = new FileInfo("SaperRecords.txt");

                inStream = textFile.OpenText();
                line1 = inStream.ReadLine();
                line2 = inStream.ReadLine();
                line3 = inStream.ReadLine();

          /*      while (line1 != null)
                {
                    textLine = inStream.ReadLine();
                }*/
         /*   }
            catch (IOException exObj)
            {
                Console.WriteLine(exObj);
            }*/
          //  finally
          //  {
                inStream.Close();
          //  }
        }

        private void MyOkMouseDown(object sender, MouseEventArgs e)
        {
            // И еще надо закріть регистрацию
            userName = myTextBox.Text;
            myForm.Close();
            SaveToFile(timeCounter);
            LoadFromFile();
            ShowRecords();
            
        }

        private void MyClearMouseDown(object sender, MouseEventArgs e)
        {
            SaveToFile();
            LoadFromFile();
            myForm1.Close();
            ShowRecords();

        }

        private void MyOk1MouseDown(object sender, MouseEventArgs e)
        {
            // Close form
            myForm1.Close();
        }


        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            beginnerToolStripMenuItem.Checked = true;
            Game();
        }

        public void RemoveButtons()
        {            
            if (spButt!=null)
            for (int i = 0; i < spButt.GetLength(0); i++)
            {
                for (int j = 0; j < spButt.GetLength(1); j++)
                {
                    this.Controls.Remove(spButt[i,j]);
                    spButt[i, j].Dispose();
                }
            }
        }

        public void Game()
        {
            Form1.BombCounter = 0; 
            timeCounter = 0;
            winFlag = true;
            timer1.Enabled = false;
            timerFlag = true;
            if (beginnerToolStripMenuItem.Checked == true)
            {
                matrX = 10;
                matrY = 10;
                bombAmount = 10;
            }
        //    else
                if (amateurToolStripMenuItem.Checked == true)
                {
                    matrX = 16;
                    matrY = 16;
                    bombAmount = 40;
                }
               // else
                    if (professionalToolStripMenuItem.Checked == true)
                    {
                        matrX = 16;
                        matrY = 30;
                        bombAmount = 99;
                    }
                    //label1.Text = Convert.ToString(Form1.BombCounter);

            SpecialButton[,] butts;
            butts = new SpecialButton[matrX, matrY];

            PictureMatrix myPictureMatrix = new PictureMatrix();

            myPictureMatrix.ViewField();

            butts = myPictureMatrix.GetButtons();
            spButt = myPictureMatrix.GetButtons();
            for (int i = 0; i < butts.GetLength(0); i++)
            {
                for (int j = 0; j < butts.GetLength(1); j++)
                {
                    butts[i, j].ButtonX = i;
                    butts[i, j].ButtonY = j;
                    this.Controls.Add(butts[i, j]);
                    butts[i, j].MouseDown += new MouseEventHandler(MyMouseDown);
                }
            }

            this.Height = (butts[0, 0].Height - 1) * butts.GetLength(0) + menuStrip1.Height + 70 + 15;//70 eto dlya otstupa, +15 dlya krasoty
            this.Width = (butts[0, 0].Width - 1) * butts.GetLength(1) + 17;//17 dlya krasoty
            this.Text = "Sapper";
            button1.Left = this.Width / 2 - button1.Width / 2-3;
            label1.Left = button1.Left / 2 -label1.Width/2- 3;
            label2.Left = 3*button1.Left / 2 + label2.Width / 2 - 3;
            label2.Text = "0";
            button1.Image = Image.FromFile("smile.png");
            label1.Text = Convert.ToString(Form1.BombCounter);
        }

        private void MyMouseDown(object sender, MouseEventArgs e)
        {
            if (timerFlag == true)
            {
                timer1.Enabled = true;
                timerFlag = false;
            }
            
            if (e.Button == MouseButtons.Left)
            {
                if ((sender as SpecialButton).IsOpened == false)
                {
                    if ((sender as SpecialButton).IsBomb == false)
                    {
                        (sender as SpecialButton).FlatStyle = FlatStyle.Popup;
                        (sender as SpecialButton).IsOpened = true;
                        (sender as SpecialButton).OpenZeros(spButt, (sender as SpecialButton).ButtonX, (sender as SpecialButton).ButtonY);
                      
                        if (((sender as SpecialButton).Digit != 0) && ((sender as SpecialButton).Digit != 9))
                            (sender as SpecialButton).Text = Convert.ToString((sender as SpecialButton).Digit);
                        SecretPicture.DigitColor((sender as SpecialButton));
                    }
                    else
                    {                        
                        for (int i = 0; i < spButt.GetLength(0); i++)
                        {
                            for (int j = 0; j < spButt.GetLength(1); j++)
                            {
                                if (spButt[i, j].IsBomb == true)
                                    spButt[i, j].Image = Image.FromFile("bomb.png");

                                if ((spButt[i, j].Digit != 0) && (spButt[i, j].Digit != 9))
                                    spButt[i, j].Text = Convert.ToString(spButt[i, j].Digit);
                                SecretPicture.DigitColor(spButt[i, j]);

                                spButt[i, j].FlatStyle = FlatStyle.Popup;
                                spButt[i, j].IsOpened = true;
                            }
                        }
                        (sender as SpecialButton).Image = Image.FromFile("redBomb.png");
                        button1.Image = Image.FromFile("sad.png");
                        timer1.Enabled = false;
                       // timerFlag = true;
                    }

                }
            }
            else
                if (e.Button == MouseButtons.Right)
                {
                    if ((sender as SpecialButton).IsOpened == false)
                    {
                        (sender as SpecialButton).Image = Image.FromFile("flag.png");
                        (sender as SpecialButton).IsFlag=true;
                        (sender as SpecialButton).IsOpened = true;
                        //(sender as SpecialButton).Enabled = false;
                        label1.Text = Convert.ToString(Convert.ToInt32(label1.Text) - 1);

                    }
                }

            for (int i = 0; i < spButt.GetLength(0); i++)
            {
                for (int j = 0; j < spButt.GetLength(1); j++)
                {
                    if (spButt[i, j].IsOpened == true)
                        winFlag = true;
                    else
                        winFlag = false;
                    if (winFlag==false)
                        break;
                }
            }

            int winCounter=0;
            if (winFlag == true)
            {
                for (int i = 0; i < spButt.GetLength(0); i++)
                {
                    for (int j = 0; j < spButt.GetLength(1); j++)
                    {
                        if (((spButt[i,j].IsBomb==true)&&(spButt[i,j].IsFlag==true))|| 
                            ((spButt[i,j].IsBomb!=true)&&(spButt[i,j].IsFlag!=true)))
                            winCounter++;
                    }
                }
            }
            if (winCounter == MatrX * MatrY)
            {
                timer1.Enabled = false;
                //SaveToFile(timeCounter);
                //LoadFromFile();
                ShowRegistration();
                button1.Image = Image.FromFile("win.png");
            }

        }

        private void beginnerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            
            beginnerToolStripMenuItem.Checked = true;
            amateurToolStripMenuItem.Checked = false;
            professionalToolStripMenuItem.Checked = false;
            
            RemoveButtons();
            Game();
        }

        private void newGameToolStripMenuItem_Click(object sender, EventArgs e)
        {
            RemoveButtons();
            Game();
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            RemoveButtons();
            Game();
        }

        private void amateurToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            amateurToolStripMenuItem.Checked = true;
            beginnerToolStripMenuItem.Checked = false; 
            professionalToolStripMenuItem.Checked = false;

            RemoveButtons();
            Game();

        }

        private void professionalToolStripMenuItem_Click_1(object sender, EventArgs e)
        {
            professionalToolStripMenuItem.Checked = true;
            beginnerToolStripMenuItem.Checked = false;
            amateurToolStripMenuItem.Checked = false;

            RemoveButtons();
            Game();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timeCounter++;
            label2.Text = Convert.ToString(timeCounter);
        }

        private void recordsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadFromFile();
            ShowRecords();
        }
    }

    class SpecialButton : Button
    {
        private double probability;
        private byte digit;
        private bool isBomb;
        private bool isFlag;
        private bool isOpened;
        private int x;
        private int y;

        public double Probability
        {
            get
            {
                return probability;
            }
            set
            {
                probability = value;
            }
        }

        public byte Digit
        {
            get
            {
                return digit;
            }
            set
            {
                digit = value;
            }
        }

        public int ButtonX
        {
            get
            {
                return x;
            }
            set
            {
                x = value;
            }
        }

        public int ButtonY
        {
            get
            {
                return y;
            }
            set
            {
                y = value;
            }
        }

        public bool IsBomb
        {
            get
            {
                return isBomb;
            }
            set
            {
                isBomb = value;
            }
        }

        public bool IsFlag
        {
            get
            {
                return isFlag;
            }
            set
            {
                isFlag = value;
            }
        }

        public bool IsOpened
        {
            get
            {
                return isOpened;
            }
            set
            {
                isOpened = value;
            }
        }

        public SpecialButton()
            : base()
        {
            this.Digit = 0;
            this.IsBomb = false;
            this.IsOpened = false;
            this.Enabled = true;
            //this.FlatStyle = FlatStyle.System;
        }

        public void OpenZeros(SpecialButton[,] zeroButts, int iClick, int jClick)
        {
            //для внутренней части поля
            if ((iClick > 0) && (iClick < zeroButts.GetLength(0) - 1) && (jClick > 0) &&
                                             (jClick < zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }

                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }
                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
                //--------
                if ((zeroButts[iClick-1, jClick - 1].Digit != 9) && (zeroButts[iClick-1, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick-1, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick-1, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick - 1].Text = Convert.ToString(zeroButts[iClick - 1, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick-1, jClick - 1);
                }
                if ((zeroButts[iClick+1, jClick - 1].Digit != 9) && (zeroButts[iClick+1, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick+1, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick+1, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick - 1].Text = Convert.ToString(zeroButts[iClick + 1, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick+1, jClick - 1);
                }
                if ((zeroButts[iClick + 1, jClick + 1].Digit != 9) && (zeroButts[iClick + 1, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick + 1].Text = Convert.ToString(zeroButts[iClick + 1, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick+1]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick + 1);
                }
                if ((zeroButts[iClick - 1, jClick + 1].Digit != 9) && (zeroButts[iClick - 1, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick -1, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick + 1].Text = Convert.ToString(zeroButts[iClick - 1, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick + 1);
                }
            }

            //для угловых точек
            //(0,0)
            if ((iClick == 0) && (jClick == 0) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick+1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }
                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
            }

            //(0,poslednyaya)
            if ((iClick == 0) && (jClick == zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }
                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
            }

            //(poslednyaya,0)
            if ((iClick == zeroButts.GetLength(0) - 1) && (jClick == 0) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }
            }

            //(poslednyaya,poslednyaya)
            if ((iClick == zeroButts.GetLength(0) - 1) && (jClick == zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }
            }

            //для линейных краев
            //для верхней
            if ((iClick == 0) && (jClick > 0) && (jClick < zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }

                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }

                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
            }

            //для нижней границы
            if ((iClick == zeroButts.GetLength(0) - 1) && (jClick > 0) && (jClick < zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }

                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }
                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
            }

            //для левого края
            if ((iClick > 0) && (iClick < zeroButts.GetLength(0) - 1) && (jClick == 0) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick + 1].Digit != 9) && (zeroButts[iClick, jClick + 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick + 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick + 1].IsOpened = true;
                    if (zeroButts[iClick, jClick + 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick + 1].Text = Convert.ToString(zeroButts[iClick, jClick + 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick + 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick + 1);
                }
                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
            }

            //
            if ((iClick > 0) && (iClick < zeroButts.GetLength(0) - 1) && (jClick == zeroButts.GetLength(1) - 1) && (zeroButts[iClick, jClick].Digit == 0))
            {
                if ((zeroButts[iClick, jClick - 1].Digit != 9) && (zeroButts[iClick, jClick - 1].IsOpened == false))
                {
                    zeroButts[iClick, jClick - 1].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick, jClick - 1].IsOpened = true;
                    if (zeroButts[iClick, jClick - 1].Digit != 0)
                    {
                        zeroButts[iClick, jClick - 1].Text = Convert.ToString(zeroButts[iClick, jClick - 1].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick, jClick - 1]);
                    }
                    OpenZeros(zeroButts, iClick, jClick - 1);
                }

                if ((zeroButts[iClick - 1, jClick].Digit != 9) && (zeroButts[iClick - 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick - 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick - 1, jClick].IsOpened = true;
                    if (zeroButts[iClick - 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick - 1, jClick].Text = Convert.ToString(zeroButts[iClick - 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick-1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick - 1, jClick);
                }
                if ((zeroButts[iClick + 1, jClick].Digit != 9) && (zeroButts[iClick + 1, jClick].IsOpened == false))
                {
                    zeroButts[iClick + 1, jClick].FlatStyle = FlatStyle.Popup;
                    zeroButts[iClick + 1, jClick].IsOpened = true;
                    if (zeroButts[iClick + 1, jClick].Digit != 0)
                    {
                        zeroButts[iClick + 1, jClick].Text = Convert.ToString(zeroButts[iClick + 1, jClick].Digit);
                        SecretPicture.DigitColor(zeroButts[iClick+1, jClick]);
                    }
                    OpenZeros(zeroButts, iClick + 1, jClick);
                }
            }
        }
    }

    class PictureMatrix
    {        
        private SpecialButton[,] buttons = new SpecialButton[Form1.MatrX, Form1.MatrY];

        

        public void ViewField()
        {
            for (int i = 0; i < buttons.GetLength(0); i++)
            {
                for (int j = 0; j < buttons.GetLength(1); j++)
                {
                    buttons[i, j] = new SpecialButton();

                }
            }

            for (int i = 0; i < buttons.GetLength(0); i++)
            {
                for (int j = 0; j < buttons.GetLength(1); j++)
                {
                    buttons[i, j].Width = 25;
                    buttons[i, j].Height = 25;
                    buttons[i, j].Left = j *( buttons[i, j].Width-1);
                    buttons[i, j].Top = i * (buttons[i, j].Height-1)+70;  //70 eto otstup
                }
            }

            SecretPicture mySecretPicture = new SecretPicture();
            mySecretPicture.DrawBomb(buttons);
            mySecretPicture.DrawDigits(buttons);

            /* for (int i = 0; i < buttons.GetLength(0); i++)
            {
                for (int j = 0; j < buttons.GetLength(1); j++)
                {
                    if ((buttons[i,j].Digit!=0)&&(buttons[i,j].Digit!=9))
                        buttons[i, j].Text= Convert.ToString(buttons[i, j].Digit);
                    SecretPicture.DigitColor(buttons[i,j]);
                }
            }*/
        }

        public SpecialButton[,] GetButtons()
        {
            return buttons;
        }
    }

    class SecretPicture
    {


        public void DrawBomb(SpecialButton[,] myButts)
        {
            //const byte BombAmount = 10;
            Form1.BombCounter = 0;
            SpecialButton[,] butts = new SpecialButton[Form1.MatrX, Form1.MatrY];
            butts = myButts;

            Random randomizer = new Random();
            for (int i = 0; i < butts.GetLength(0); i++)
            {
                for (int j = 0; j < butts.GetLength(1); j++)
                {
                    butts[i, j].Probability = (double)randomizer.Next(1, 100);
                }
            }

            for (int i = 0; i < butts.GetLength(0); i++)
            {
                for (int j = 0; j < butts.GetLength(1); j++)
                {
                    if (Form1.BombCounter < Form1.BombAmount)
                    {
                        if (butts[i, j].Probability > 90.0)
                        {
                           // butts[i, j].Image = Image.FromFile(@"C:\bomb.png");
                            butts[i, j].IsBomb = true;
                            Form1.BombCounter++;
                        }
                    }
                }
            }
        }

        public static void DigitColor(SpecialButton myButt)
        {
            SpecialButton butt = new SpecialButton();
            butt = myButt;

            if (butt.Digit == 1)
                butt.ForeColor = Color.Blue;
            else
                if (butt.Digit == 2)
                    butt.ForeColor = Color.Green;
                else
                    if (butt.Digit == 3)
                        butt.ForeColor = Color.Red;
                    else
                        if (butt.Digit == 4)
                            butt.ForeColor = Color.Teal;
                        else
                            if (butt.Digit == 5)
                                butt.ForeColor = Color.Maroon;
                            else
                                if (butt.Digit == 6)
                                    butt.ForeColor = Color.Magenta;
                                else
                                    if (butt.Digit == 7)
                                        butt.ForeColor = Color.Gold;
                                    else
                                        if (butt.Digit == 8)
                                            butt.ForeColor = Color.Brown;
        }


        public void DrawDigits(SpecialButton[,] myButts)
        {
            SpecialButton[,] butts = new SpecialButton[Form1.MatrX, Form1.MatrY];
            butts = myButts;


            if (butts[0, 0].IsBomb == true)
            {
                butts[0, 1].Digit = (byte)(butts[0, 1].Digit + 1);
                butts[1, 0].Digit = (byte)(butts[1, 0].Digit + 1);
                butts[1, 1].Digit = (byte)(butts[1, 1].Digit + 1);
            }

            if (butts[0, butts.GetLength(1) - 1].IsBomb == true)
            {
                butts[0, butts.GetLength(1) - 2].Digit = (byte)(butts[0, butts.GetLength(1) - 2].Digit + 1);
                butts[1, butts.GetLength(1) - 1].Digit = (byte)(butts[1, butts.GetLength(1) - 1].Digit + 1);
                butts[1, butts.GetLength(1) - 2].Digit = (byte)(butts[1, butts.GetLength(1) - 2].Digit + 1);
            }

            if (butts[butts.GetLength(0) - 1, 0].IsBomb == true)
            {
                butts[butts.GetLength(0) - 2, 0].Digit = (byte)(butts[butts.GetLength(0) - 2, 0].Digit + 1);
                butts[butts.GetLength(0) - 2, 1].Digit = (byte)(butts[butts.GetLength(0) - 2, 1].Digit + 1);
                butts[butts.GetLength(0) - 1, 1].Digit = (byte)(butts[butts.GetLength(0) - 1, 1].Digit + 1);
            }

            if (butts[butts.GetLength(0) - 1, butts.GetLength(1) - 1].IsBomb == true)
            {
                butts[butts.GetLength(0) - 2, butts.GetLength(1) - 2].Digit = (byte)(butts[butts.GetLength(0) - 2, butts.GetLength(1) - 2].Digit + 1);
                butts[butts.GetLength(0) - 1, butts.GetLength(1) - 2].Digit = (byte)(butts[butts.GetLength(0) - 1, butts.GetLength(1) - 2].Digit + 1);
                butts[butts.GetLength(0) - 2, butts.GetLength(1) - 1].Digit = (byte)(butts[butts.GetLength(0) - 2, butts.GetLength(1) - 1].Digit + 1);
            }

            for (int j = 1; j < butts.GetLength(1) - 1; j++)
            {
                if (butts[0, j].IsBomb == true)
                {
                    butts[0, j - 1].Digit = (byte)(butts[0, j - 1].Digit + 1);
                    butts[0, j + 1].Digit = (byte)(butts[0, j + 1].Digit + 1);
                    butts[1, j - 1].Digit = (byte)(butts[1, j - 1].Digit + 1);
                    butts[1, j].Digit = (byte)(butts[1, j].Digit + 1);
                    butts[1, j + 1].Digit = (byte)(butts[1, j + 1].Digit + 1);
                }

                if (butts[butts.GetLength(0) - 1, j].IsBomb == true)
                {
                    butts[butts.GetLength(0) - 1, j - 1].Digit = (byte)(butts[butts.GetLength(0) - 1, j - 1].Digit + 1);
                    butts[butts.GetLength(0) - 1, j + 1].Digit = (byte)(butts[butts.GetLength(0) - 1, j + 1].Digit + 1);
                    butts[butts.GetLength(0) - 2, j - 1].Digit = (byte)(butts[butts.GetLength(0) - 2, j - 1].Digit + 1);
                    butts[butts.GetLength(0) - 2, j].Digit = (byte)(butts[butts.GetLength(0) - 2, j].Digit + 1);
                    butts[butts.GetLength(0) - 2, j + 1].Digit = (byte)(butts[butts.GetLength(0) - 2, j + 1].Digit + 1);
                }
            }

            for (int i = 1; i < butts.GetLength(0) - 1; i++)
            {
                if (butts[i, 0].IsBomb == true)
                {
                    butts[i - 1, 0].Digit = (byte)(butts[i - 1, 0].Digit + 1);
                    butts[i + 1, 0].Digit = (byte)(butts[i + 1, 0].Digit + 1);
                    butts[i - 1, 1].Digit = (byte)(butts[i - 1, 1].Digit + 1);
                    butts[i, 1].Digit = (byte)(butts[i, 1].Digit + 1);
                    butts[i + 1, 1].Digit = (byte)(butts[i + 1, 1].Digit + 1);
                }

                if (butts[i, butts.GetLength(1) - 1].IsBomb == true)
                {
                    butts[i - 1, butts.GetLength(1) - 1].Digit = (byte)(butts[i - 1, butts.GetLength(1) - 1].Digit + 1);
                    butts[i + 1, butts.GetLength(1) - 1].Digit = (byte)(butts[i + 1, butts.GetLength(1) - 1].Digit + 1);
                    butts[i - 1, butts.GetLength(1) - 2].Digit = (byte)(butts[i - 1, butts.GetLength(1) - 2].Digit + 1);
                    butts[i, butts.GetLength(1) - 2].Digit = (byte)(butts[i, butts.GetLength(1) - 2].Digit + 1);
                    butts[i + 1, butts.GetLength(1) - 2].Digit = (byte)(butts[i + 1, butts.GetLength(1) - 2].Digit + 1);
                }
            }

            for (int i = 1; i < butts.GetLength(0) - 1; i++)
            {
                for (int j = 1; j < butts.GetLength(1) - 1; j++)
                {
                    if (butts[i, j].IsBomb == true)
                    {
                        butts[i - 1, j - 1].Digit = (byte)(butts[i - 1, j - 1].Digit + 1);
                        butts[i, j - 1].Digit = (byte)(butts[i, j - 1].Digit + 1);
                        butts[i + 1, j - 1].Digit = (byte)(butts[i + 1, j - 1].Digit + 1);
                        butts[i - 1, j].Digit = (byte)(butts[i - 1, j].Digit + 1);
                        butts[i + 1, j].Digit = (byte)(butts[i + 1, j].Digit + 1);
                        butts[i - 1, j + 1].Digit = (byte)(butts[i - 1, j + 1].Digit + 1);
                        butts[i, j + 1].Digit = (byte)(butts[i, j + 1].Digit + 1);
                        butts[i + 1, j + 1].Digit = (byte)(butts[i + 1, j + 1].Digit + 1);
                    }
                }
            }

            //Специально присваиваю бомбочкам левое значение 9, чтобы у них не было значение 0, и они не проверялись как нули
            for (int i = 0; i < butts.GetLength(0); i++)
            {
                for (int j = 0; j < butts.GetLength(1); j++)
                {
                    if (butts[i, j].IsBomb == true)
                        butts[i, j].Digit = 9;
                }
            }
        }
    }



}
